<template>
  <div class="start-charging-container">
    <div class="charging-card">
      <div class="card-header">
        <h2>开始充电</h2>
        <p>确认开始为您的车辆充电</p>
      </div>

      <div class="charging-status">
        <div class="status-icon">
          <el-icon class="icon-pulse"><Lightning /></el-icon>
        </div>
        <div class="status-info">
          <h3>准备就绪</h3>
          <p>您的车辆已连接，可以开始充电</p>
        </div>
      </div>

      <a-form 
        @submit.prevent="submit"
        class="charging-form"
        :model="formState"
      >
        <a-form-item label="车牌号">
          <a-input 
            v-model:value="formState.car_id"
            class="form-input"
          >
            <template #prefix>
              <el-icon><Van /></el-icon>
            </template>
          </a-input>
        </a-form-item>

        <a-form-item>
          <a-button 
            type="primary" 
            html-type="submit"
            class="start-button"
            :loading="loading"
          >
            <template #icon><el-icon><VideoPlay /></el-icon></template>
            开始充电
          </a-button>
        </a-form-item>
      </a-form>

      <div class="charging-tips">
        <h4>充电提示</h4>
        <ul>
          <li>请确保车辆已正确连接到充电桩</li>
          <li>充电过程中请勿断开连接</li>
          <li>如需停止充电，请使用停止充电功能</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { Lightning, Van, VideoPlay } from '@element-plus/icons-vue'

export default {
  components: {
    Lightning,
    Van,
    VideoPlay
  },
  setup() {
    const loading = ref(false)
    const formState = reactive({
      car_id: sessionStorage.getItem("car_id")
    })

    const submit = async () => {
      loading.value = true
      try {
        const response = await fetch(process.env.VUE_APP_BACKEND_URL + '/user/startCharging', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            car_id: formState.car_id
          })
        })

        const data = await response.json()
        if (data && data.code === 0) {
          message.success('充电已开始')
        } else {
          message.error(data.msg || '启动充电失败')
        }
      } catch (error) {
        message.error(error.message || '网络错误，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    return {
      formState,
      loading,
      submit
    }
  }
}
</script>

<style scoped>
.start-charging-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  background: var(--background-light);
}

.charging-card {
  width: 100%;
  max-width: 480px;
  background: white;
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 32px;
  animation: slideUp 0.5s ease;
}

.card-header {
  text-align: center;
  margin-bottom: 32px;
}

.card-header h2 {
  color: var(--primary-color);
  font-size: 24px;
  margin-bottom: 8px;
}

.card-header p {
  color: var(--text-gray);
  font-size: 16px;
}

.charging-status {
  display: flex;
  align-items: center;
  gap: 20px;
  background: rgba(43, 51, 145, 0.05);
  padding: 20px;
  border-radius: var(--border-radius-md);
  margin-bottom: 32px;
}

.status-icon {
  width: 48px;
  height: 48px;
  background: var(--gradient-primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-icon .el-icon {
  font-size: 24px;
  color: white;
}

.icon-pulse {
  animation: pulse 2s infinite;
}

.status-info h3 {
  color: var(--primary-color);
  margin: 0 0 4px 0;
  font-size: 18px;
}

.status-info p {
  color: var(--text-gray);
  margin: 0;
  font-size: 14px;
}

.charging-form {
  margin-bottom: 32px;
}

.form-input {
  width: 100%;
}

.start-button {
  width: 100%;
  height: 44px;
  background: var(--gradient-primary);
  border: none;
  border-radius: var(--border-radius-sm);
  font-size: 16px;
  font-weight: 500;
  transition: all var(--transition-fast);
}

.start-button:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.start-button:active {
  transform: translateY(0);
}

.charging-tips {
  background: rgba(43, 51, 145, 0.05);
  padding: 20px;
  border-radius: var(--border-radius-md);
}

.charging-tips h4 {
  color: var(--primary-color);
  margin: 0 0 12px 0;
  font-size: 16px;
}

.charging-tips ul {
  margin: 0;
  padding-left: 20px;
}

.charging-tips li {
  color: var(--text-gray);
  margin-bottom: 8px;
  font-size: 14px;
}

.charging-tips li:last-child {
  margin-bottom: 0;
}

:deep(.ant-form-item-label > label) {
  color: var(--text-dark);
  font-weight: 500;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: var(--border-radius-sm);
  border-color: #dcdfe6;
  transition: all var(--transition-fast);
}

:deep(.ant-input-affix-wrapper:hover) {
  border-color: var(--primary-color);
}

:deep(.ant-input-affix-wrapper-focused) {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(43, 51, 145, 0.1);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .start-charging-container {
    padding: 16px;
  }

  .charging-card {
    padding: 24px;
  }

  .card-header h2 {
    font-size: 20px;
  }

  .card-header p {
    font-size: 14px;
  }

  .charging-status {
    padding: 16px;
  }

  .status-icon {
    width: 40px;
    height: 40px;
  }

  .status-icon .el-icon {
    font-size: 20px;
  }

  .status-info h3 {
    font-size: 16px;
  }

  .status-info p {
    font-size: 12px;
  }
}
</style>
